package mbean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import dao.BSCPerspectiveDAO;

import util.FacesUtil;
import vo.BSCPerspectiveVO;
import vo.PerspectiveVO;

public class BSCPerspectiveMBean extends AbstractMBean{

	private List<String> listaPerspectives, listaUsedPerspectives;
	private Map<String, PerspectiveVO> mapaPerspective;
	private BSCPerspectiveDAO dao;
	private Map<String, BSCPerspectiveVO> mapaUsedPerspective;
	
	public String open() {
		this.listaUsedPerspectives = new ArrayList<String>();
		this.listaPerspectives = new ArrayList<String>();
				
		dao = new BSCPerspectiveDAO();
		mapaPerspective = dao.getPerspectives(getStrategicPlanId());		
		mapaUsedPerspective = dao.getUsedPerspectives(getStrategicPlanId());
		
		List<BSCPerspectiveVO> tmp = new ArrayList<BSCPerspectiveVO>();
		for(String key:mapaUsedPerspective.keySet()){
			tmp.add(mapaUsedPerspective.get(key));
		}
		
		Collections.sort(tmp, new Comparator<BSCPerspectiveVO>(){
			public int compare(BSCPerspectiveVO o1, BSCPerspectiveVO o2) {
				if (o1.getPosition() < o2.getPosition())
					return -1;
				else if (o1.getPosition() == o2.getPosition())
					return 0;
				else
					return 1;			
			}			
		});		
		
		for(BSCPerspectiveVO pvo:tmp){			 			
			this.listaUsedPerspectives.add(Integer.toString(pvo.getPerspectiveId()));
		}
		
		for(String key:mapaPerspective.keySet()){
			this.listaPerspectives.add(key);
		}
		
		this.listaPerspectives.removeAll(this.listaUsedPerspectives);
		setaTela(true, "bscPerspective");
		
		return "strategic-plan-perspective";
	}

	public List<String> getListaPerspectives() {
		return listaPerspectives;
	}

	public void setListaPerspectives(List<String> listaPerspectives) {
		this.listaPerspectives = listaPerspectives;
	}

	public List<String> getListaUsedPerspectives() {
		return listaUsedPerspectives;
	}

	public void setListaUsedPerspectives(List<String> listaUsedPerspectives) {
		this.listaUsedPerspectives = listaUsedPerspectives;
	}

	public Map<String, PerspectiveVO> getMapaPerspective() {
		return mapaPerspective;
	}

	public void setMapaPerspective(Map<String, PerspectiveVO> mapaPerspective) {
		this.mapaPerspective = mapaPerspective;
	}
	
	public int getStrategicPlanId() {
		return ((StrategyMBean) FacesUtil.getSessionMapValue("strategy")).getStrategicPlanId();
	}
	
	public void save(){
		if (this.listaUsedPerspectives.size() <= 0){
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("errorPerspectivesRequired"));
		}
		else{			
			this.dao.save(this.listaUsedPerspectives, getStrategicPlanId());
			MenuMBean mmb = (MenuMBean) FacesUtil.getSessionMapValue("menu");
			mmb.loadTree();
		}
	}

	public void usePerspective(int idPerspective) {
		open();
		this.listaUsedPerspectives.add(Integer.toString(idPerspective));
		this.listaPerspectives.remove(Integer.toString(idPerspective));
	}

	@Override
	public boolean isDirty() {
		boolean dirty = false;
		
		List<BSCPerspectiveVO> listaTmp = new ArrayList<BSCPerspectiveVO>();
		for(String key:mapaUsedPerspective.keySet()){
			listaTmp.add(mapaUsedPerspective.get(key));
		}
		
		Collections.sort(listaTmp, new Comparator<BSCPerspectiveVO>(){
			public int compare(BSCPerspectiveVO o1, BSCPerspectiveVO o2) {
				if (o1.getPosition() < o2.getPosition())
					return -1;
				else if (o1.getPosition() == o2.getPosition())
					return 0;
				else
					return 1;			
			}			
		});		
		
		List<String> lista = new ArrayList<String>();
		for(BSCPerspectiveVO pvo:listaTmp){			 			
			lista.add(Integer.toString(pvo.getPerspectiveId()));
		}
		
		if (! lista.equals(this.listaUsedPerspectives))
			dirty = true;
		else{
			for(int i = 0; ((i < lista.size()) || (dirty)); i++){
				if (! lista.get(i).equalsIgnoreCase(this.listaUsedPerspectives.get(i)))
					dirty = true;
			}
		}
		
		return dirty;
	}
}
